/**
 * Alibaba.com.cn Inc.
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.smart.helper;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.google.common.collect.Lists;
import com.smart.common.utils.AssertUtil;
import com.smart.mapper.ProjectFileTaskMapper;
import com.smart.mapper.ProjectMapper;
import com.smart.model.convert.ProjectConvert;
import com.smart.model.domain.ProjectDO;
import com.smart.model.domain.ProjectFileTaskDO;
import com.smart.model.domain.ProjectFileTaskInstanceDO;
import com.smart.model.request.ProjectFileTaskRequest;
import com.smart.model.vo.ProjectVO;
import com.smart.service.ProjectFileTaskInstanceService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhanghong
 * @version FileHelper.java, v 0.1 2023/7/20 00:24 下午 FileHelper Exp $$
 */
@Component
public class FileHelper {

    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectFileTaskMapper projectFileTaskMapper;
    @Autowired
    private ProjectFileTaskInstanceService projectFileTaskInstanceService;

    /**
     * 查询导出数据
     * @param request
     * @return
     */
    public Pair<String, List<ProjectVO>> queryExportInfo(ProjectFileTaskRequest request) {
        ProjectFileTaskDO projectFileTaskDO = projectFileTaskMapper.selectDOById(request.getId());
        AssertUtil.assertNotNull(projectFileTaskDO, "数据不存在");

        // 查询接收文件数据，按照接收时间倒序
        LambdaQueryChainWrapper<ProjectFileTaskInstanceDO> wrapper = projectFileTaskInstanceService.lambdaQuery()
                .eq(ProjectFileTaskInstanceDO::getTaskId, projectFileTaskDO.getId())
                .orderByDesc(ProjectFileTaskInstanceDO::getReceiveTime);
        List<ProjectFileTaskInstanceDO> sourceList = wrapper.list();
        if (request.getReceiveStatus() != null) {
            //接收/未接收-条件过滤
            wrapper.eq(ProjectFileTaskInstanceDO::getStatus, request.getReceiveStatus());
        }
        List<ProjectFileTaskInstanceDO> list = wrapper.list();

        if (CollectionUtils.isEmpty(list)) {
            return Pair.of(getFileName(request), Collections.emptyList());
        }

        Set<Long> projects = list.stream().map(ProjectFileTaskInstanceDO::getProjectId).collect(Collectors.toSet());
        request.setProjectIds(projects);
        List<ProjectDO> projectDOS = projectMapper.fileExportListQuery(request);
        List<ProjectVO> projectVOS = ProjectConvert.buildProjectVoList(projectDOS);
        projectVOS.forEach(item -> {
            item.setFileTaskId(projectFileTaskDO.getId());
        });

        return Pair.of(getFileName(request), projectVOS);
    }



    private String getFileName(ProjectFileTaskRequest request) {
        // 前置校验
        ProjectFileTaskDO taskDO = projectFileTaskMapper.selectDOById(request.getId());
        AssertUtil.assertNotNull(taskDO, "文件任务不存在");
        String receiveStatus = request.getReceiveStatus() == 0 ? "未接收" : "已接收";
        String date = DateUtil.format(new Date(), DatePattern.PURE_DATE_PATTERN);
        String fileName = String.format("[%s-%s][%s]项目名单-[%s]", taskDO.getFileCode(), taskDO.getFileName(), receiveStatus, date);
        return fileName;
    }
}
